/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {
    //上传合同扫描件
    $('.upload-contract-scan').ajaxUpload({
        url: '/v1/upload',
        name:'contract-scan',
        top: '190px',
        onSubmit:function (data){
            $('#text-contract-scan').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];
                $('.upload-contract-scan>img').attr('src','/images/ok-logo.png');
                $('.input-contract-scan').val(info.savename);
                $('#text-contract-scan').html('上传合同扫描件');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    //-----------------------------------------------------------------------------

    var toolbar = '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-2">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-10">';
    toolbar += '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-6">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-6">';
    toolbar += '<select class="form-control select-process"><option value="">进度</option><option value="1">创建</option><option value="2">身份认证</option><option value="3">身份审核</option><option value="4">支付押金</option><option value="5">完成</option></select>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';

    loadList('/v1/manager/contract/list.json', toolbar);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-process');

    $('#view-contract-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var process = button.data('process') + 1;
        $('.contract-id').val(id);
        $('.contract-process').val(process);
        $('.contract-step li').removeClass('active');
        $('.contract-step li').each(function(n){
            if (process > n) {
                $(this).addClass("active");
            }
        });
        if (process == 1) {
            $('.wait-upload').removeClass('hidden');
            $('.modal-footer .btn-save').addClass('hidden');
        }
        if (process == 2) {
            $('.upload-identity').removeClass('hidden');
            $('.check-identity').removeClass('hidden');
        }
        if (process == 3) {
            $('.pay-deposit').removeClass('hidden');
            $('.modal-footer .btn-save').addClass('hidden');
        }
        if (process == 4) {
            $('.check-deposit').removeClass('hidden');
            $('.modal-footer .btn-save').removeClass('hidden');
        }
        if (process == 5) {
            $('.modal-footer .btn-save').addClass('hidden');
        }
        var that = $(this);
        sendAjax('/v1/manager/contract/detail.json', 'get', {id:id}, function(json){
            var contract = json.body.data;
            that.find('.contract-sn').text(contract.sn);
            that.find('.contract-apart').text(contract.apart_name);
            that.find('.contract-deposit').text(contract.deposit);
            that.find('.contract-order-sn').text(contract.pay_order);
            that.find('.start-end-time').text(contract.start_at+' ~ '+contract.end_at);
            that.find('.contract-remark').text(contract.remark);
            if (contract.attachment != '') {
                var attachment = JSON.parse(contract.attachment);
                if (process == 2) {
                    $('.view-identity-front').attr('href', '/pictures/'+attachment.identity_front);
                    $('.view-identity-back').attr('href', '/pictures/'+attachment.identity_back);
                    $('.img-identity-front').attr('src', '/pictures/'+attachment.identity_front);
                    $('.img-identity-back').attr('src', '/pictures/'+attachment.identity_back);
                }
                if (process > 2) {
                    $('.user-identity-info a').attr('href', '/contract/'+contract.id+'/identity.html?role=5&_token='+$.cookie()._token);
                    $('.contract-attachment-info').removeClass('hidden');
                }
                if (process > 4) {
                    $('.attachment-info a').attr('href', '/files/'+attachment.contract_file);
                    $('.contract-attachment-info').removeClass('hidden');
                }
            }
        });

    });
    $('#view-contract-dialog').on('hide.bs.modal', function(){
        $('.contract-id').val('');
        $('.contract-process').val('');
    });

    $('#view-contract-dialog .btn-save').on('click', function(){
        var data = {
            id: $('.contract-id').val(),
            process: $('.contract-process').val()
        };
        if (data.process == 3) {
            if ($('input[name="check-identity"]:checked').val() == 0) {
                data.process = 1;
            }
        }
        if (data.process == 4) {
            data.attachment = JSON.stringify({"contract_file":$('.input-contract-scan').val()});
        }

        if (data.process < 5) {
            sendAjax('/v1/manager/change/contract/process', 'post', {data:data}, function(json){
                $('#view-contract-dialog').modal('hide');
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        } else {
            $('#view-contract-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text('合同已完成');
            $('#action-tip').modal('show');
        }
    });

    $('#delete-contract-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        $(this).find('.contract-id').val(id);
    });
    $('#delete-contract-dialog').on('hide.bs.modal', function(){
        $(this).find('.contract-id').val('');
    });

    $('#delete-contract-dialog .btn-save').on('click', function(){
        var id = $('#delete-contract-dialog').find('.contract-id').val();
        sendAjax('/v1/manager/delete/contract', 'post', {id:id}, function(json){
            $('#delete-contract-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#edit-retire-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var sn = button.data('sn');
        var apart_id = button.data('apart-id');
        var apart_name = button.data('apart-name');
        var user_id = button.data('user-id');
        var realname = button.data('realname');
        $(this).find('.contract-id').val(id);
        $(this).find('.contract-sn').text(sn);
        $(this).find('.apart-id').val(apart_id);
        $(this).find('.apart-name').text(apart_name);
        $(this).find('.contract-user-id').val(user_id);
        $(this).find('.contract-realname').text(realname);
        var that = $(this);
        sendAjax('/v1/manager/retire/detail.json', 'get', {contract_id:id}, function(json){
            var retire = json.body.data;
            if (retire) {
                that.find('.retire-id').val(retire.id);
                that.find('.modal-title').text('编辑退房');
                that.find('input[name="retire-type"]').each(function(){
                    if ($(this).val() == retire.type) {
                        $(this).prop('checked', true);
                    }
                });
                that.find('input[name="is-rent-pay"]').each(function(){
                    if ($(this).val() == retire.is_rent_pay) {
                        $(this).prop('checked', true);
                    }
                });
                that.find('input[name="is-water-fee-pay"]').each(function(){
                    if ($(this).val() == retire.is_water_fee_pay) {
                        $(this).prop('checked', true);
                    }
                });
                that.find('input[name="is-electric-fee-pay"]').each(function(){
                    if ($(this).val() == retire.is_electric_fee_pay) {
                        $(this).prop('checked', true);
                    }
                });
                that.find('input[name="is-gas-fee-pay"]').each(function(){
                    if ($(this).val() == retire.is_gas_fee_pay) {
                        $(this).prop('checked', true);
                    }
                });
                that.find('input[name="is-device-safe"]').each(function(){
                    if ($(this).val() == retire.is_device_safe) {
                        $(this).prop('checked', true);
                    }
                });
                that.find('input[name="is-rent-pay"]').each(function(){
                    if ($(this).val() == retire.is_rent_pay) {
                        $(this).prop('checked', true);
                    }
                });
                that.find('.retire-remark').val(retire.remark);
            }
        });
    });
    $('#edit-retire-dialog').on('hide.bs.modal', function(){
        $(this).find('.modal-title').text('发起退房');
        $(this).find('.retire-id').val('');
        $(this).find('.contract-id').val('');
        $(this).find('.contract-sn').text('');
        $(this).find('.apart-id').val('');
        $(this).find('.apart-name').text('');
        $(this).find('.contract-user-id').val('');
        $(this).find('.contract-realname').text('');
        $(this).find('.retire-remark').val('');
    });

    $('#edit-retire-dialog .btn-save').on('click', function(){
        var that = $('#edit-retire-dialog');
        var data = {
            id: that.find('.retire-id').val(),
            contract_id: that.find('.contract-id').val(),
            apart_id : that.find('.apart-id').val(),
            user_id : that.find('.contract-user-id').val(),
            type: that.find('input[name="retire-type"]:checked').val(),
            is_rent_pay: that.find('input[name="is-rent-pay"]:checked').val(),
            is_water_fee_pay: that.find('input[name="is-water-fee-pay"]:checked').val(),
            is_electric_fee_pay: that.find('input[name="is-electric-fee-pay"]:checked').val(),
            is_gas_fee_pay: that.find('input[name="is-gas-fee-pay"]:checked').val(),
            is_device_safe: that.find('input[name="is-device-safe"]:checked').val(),
            remark: that.find('.retire-remark').val()
        };
        sendAjax('/v1/manager/save/retire', 'post', {data:data}, function(json){
            $('#edit-retire-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url, toolbar) {
    $("#contract-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        search: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.process = $('.select-process').val();
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'sn',
            title: '合同号'
        }, {
            field: 'apart_name',
            title: '房源名称'
        }, {
            field: 'realname',
            title: '承租人'
        }, {
            field: 'start_end_time',
            title: '合同起止时间'
        }, {
            field: 'order_at',
            title: '合同签订时间'
        }, {
            field: 'process',
            title: '进度',
            formatter: function (value) {
                switch (value) {
                    case 0 : return '创建';
                    case 1 : return '身份认证';
                    case 2 : return '身份审核';
                    case 3 : return '支付押金';
                    case 4 : return '完成';
                    default: return '创建';
                }
            }
        }, {
            field: 'status',
            title: '状态',
            formatter: function (value) {
                switch (value) {
                    case 1 : return '正常';
                    case 2 : return '已过期';
                    default : return '正常';
                }
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-contract" data-toggle="modal" data-target="#view-contract-dialog" data-id="'+value+'" data-process="'+row.process+'">详情</a>';
                action += '<a class="btn btn-xs btn-success edit-contract" href="/manager/contract/edit/'+value+'.html">编辑</a>';
                action += '<a class="btn btn-xs btn-danger delete-contract" data-toggle="modal" data-target="#delete-contract-dialog" data-id="'+value+'">删除</a>';
                if (row.status == 1 && row.process == 4) action += '<a class="btn btn-xs btn-warning edit-retire" data-toggle="modal" data-target="#edit-retire-dialog" data-id="'+value+'" data-sn="'+row.sn+'" data-apart-id="'+row.apart_id+'" data-apart-name="'+row.apart_name+'" data-user-id="'+row.user_id+'" data-realname="'+row.realname+'">退房</a>';
                return action;
            }
        }, {
            field: 'apart_id',
            title: '房源ID',
            visible: false
        }, {
            field: 'user_id',
            title: '用户ID',
            visible: false
        }, {
            field: 'realname',
            title: '用户姓名',
            visible: false
        }]
    });
}

function refreshList() {
    $('#contract-table').bootstrapTable('refresh');
}